package com.aps.orchestrator.scenario;

import com.aps.common.model.APSProductModel;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

/**
 * 生产场景分析结果
 * 用于描述生产环境的特征和复杂度
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductionScenario {
    
    /**
     * 生产规模
     */
    private ProductionScale scale;
    
    /**
     * 生产复杂度
     */
    private ProductionComplexity complexity;
    
    /**
     * 动态性级别
     */
    private DynamicLevel dynamicLevel;
    
    /**
     * 订单数量
     */
    private int orderCount;
    
    /**
     * 工序数量
     */
    private int operationCount;
    
    /**
     * 资源数量
     */
    private int resourceCount;
    
    /**
     * 约束数量
     */
    private int constraintCount;
    
    /**
     * 是否为多目标优化
     */
    private boolean multiObjective;
    
    /**
     * 是否为实时排程
     */
    private boolean realTime;
    
    /**
     * 场景特征描述
     */
    private String description;
    
    /**
     * 生产规模枚举
     */
    public enum ProductionScale {
        SMALL("小规模", 1, 50),      // 1-50个订单
        MEDIUM("中等规模", 51, 200),  // 51-200个订单
        LARGE("大规模", 201, 1000),   // 201-1000个订单
        EXTREME("超大规模", 1001, Integer.MAX_VALUE); // 1000+个订单
        
        private final String description;
        private final int minOrders;
        private final int maxOrders;
        
        ProductionScale(String description, int minOrders, int maxOrders) {
            this.description = description;
            this.minOrders = minOrders;
            this.maxOrders = maxOrders;
        }
        
        public String getDescription() {
            return description;
        }
        
        public boolean matches(int orderCount) {
            return orderCount >= minOrders && orderCount <= maxOrders;
        }
        
        public static ProductionScale fromOrderCount(int orderCount) {
            for (ProductionScale scale : values()) {
                if (scale.matches(orderCount)) {
                    return scale;
                }
            }
            return EXTREME;
        }
    }
    
    /**
     * 生产复杂度枚举
     */
    public enum ProductionComplexity {
        SIMPLE("简单", 1, 5),        // 1-5个工序
        MODERATE("中等", 6, 15),     // 6-15个工序
        COMPLEX("复杂", 16, 50),     // 16-50个工序
        EXTREME("极复杂", 51, Integer.MAX_VALUE); // 50+个工序
        
        private final String description;
        private final int minOperations;
        private final int maxOperations;
        
        ProductionComplexity(String description, int minOperations, int maxOperations) {
            this.description = description;
            this.minOperations = minOperations;
            this.maxOperations = maxOperations;
        }
        
        public String getDescription() {
            return description;
        }
        
        public boolean matches(int operationCount) {
            return operationCount >= minOperations && operationCount <= maxOperations;
        }
        
        public static ProductionComplexity fromOperationCount(int operationCount) {
            for (ProductionComplexity complexity : values()) {
                if (complexity.matches(operationCount)) {
                    return complexity;
                }
            }
            return EXTREME;
        }
    }
    
    /**
     * 动态性级别枚举
     */
    public enum DynamicLevel {
        STATIC("静态", "生产计划相对稳定，变化较少"),
        LOW("低动态", "偶尔有订单变更或设备故障"),
        MEDIUM("中等动态", "经常有订单插入或资源变化"),
        HIGH("高动态", "频繁的订单变更和资源调整"),
        EXTREME("极动态", "实时响应各种变化和突发事件");
        
        private final String description;
        private final String characteristics;
        
        DynamicLevel(String description, String characteristics) {
            this.description = description;
            this.characteristics = characteristics;
        }
        
        public String getDescription() {
            return description;
        }
        
        public String getCharacteristics() {
            return characteristics;
        }
    }
    
    /**
     * 判断是否为简单场景
     * @return 是否为简单场景
     */
    public boolean isSimple() {
        return scale == ProductionScale.SMALL && 
               complexity == ProductionComplexity.SIMPLE && 
               dynamicLevel == DynamicLevel.STATIC;
    }
    
    /**
     * 判断是否为复杂场景
     * @return 是否为复杂场景
     */
    public boolean isComplex() {
        return scale == ProductionScale.LARGE || scale == ProductionScale.EXTREME ||
               complexity == ProductionComplexity.COMPLEX || complexity == ProductionComplexity.EXTREME ||
               dynamicLevel == DynamicLevel.HIGH || dynamicLevel == DynamicLevel.EXTREME;
    }
    
    /**
     * 判断是否为中等场景
     * @return 是否为中等场景
     */
    public boolean isModerate() {
        return !isSimple() && !isComplex();
    }
    
    /**
     * 获取场景复杂度评分（0-1）
     * @return 复杂度评分
     */
    public double getComplexityScore() {
        double scaleScore = scale.ordinal() / (double) ProductionScale.values().length;
        double complexityScore = complexity.ordinal() / (double) ProductionComplexity.values().length;
        double dynamicScore = dynamicLevel.ordinal() / (double) DynamicLevel.values().length;
        
        return (scaleScore + complexityScore + dynamicScore) / 3.0;
    }
}
